<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Models\Administrator;
use App\Models\UserGroup;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;

class UserGroupController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户分组';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Form::dialog('组员分配')
            ->click('.edit-form')
            ->success('Dcat.reload()'); // 编辑成功后刷新页面

        return Grid::make(new UserGroup(), function (Grid $grid) {
            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->text('name', '名称');
            });
            $grid->column('id', __('Id'));
            $grid->column('name', __('名称'));
            $grid->column('users', __('group.users'))->display(function ($value) {
                $editPage = admin_base_path("user-groups/{$this->id}/edit-users");

                $str  = "";
                $more = 0;
                foreach ($value as $key => $item) {

                    if ($key < 2) {
                        $str .= "
                        <span class='label' style='background:#586cb1;'>{$item['name']}</span>
                        ";
                    } else {
                        $more++;
                    }

                }


                if ($more) {
                    $str .= "
                        <span class='label' style='background:#586cb1;'>+{$more}</span>
                        ";
                }

                if (!$str)
                    $str = '- 点击分配 -';
                return "
<div style='padding:30px 0'  data-url='{$editPage}' class='edit-form'>
   <i class='fa fa-edit'></i> {$str}
</div>
";
            });

            $grid->column('created_at', __('Created at'));
        });
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function editUsers($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->userForm()->edit($id));
    }

    protected function userForm()
    {
        return Form::make(UserGroup::with('users'), function (Form $form) {
            $form->action('user-groups/users/' . $form->getKey());
            $form->multipleSelectTable('users')
                ->from(UserTable::make())
                ->model(Administrator::class, 'id', 'name')
                ->customFormat(function ($v) {
                    if (!$v) {
                        return [];
                    }

                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                });
        });
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function updateUsers($id)
    {
        return $this->userForm()->update($id);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserGroup::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserGroup);

        $form->text('name', __('Name'));

        return $form;
    }
}
